package com.example.comm.util;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class MemberInfo {

    //TODO 此处获取当前用户编码
    public static String getUserCode() {
        return BZValue.stringValue("客户编码2");
    }

    public static String getToken() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return "";
        }
        HttpServletRequest request = requestAttributes.getRequest();
        return request.getHeader("Authorization");
    }

    private static Map<String, Object> getPlatformInfo(String jwtToken) {
        Map<String, Object> platformInfo = JwtUtils.getPayLoadMap("platform_info", jwtToken);
        if (CollectionUtils.isEmpty(platformInfo)) {
            throw new IllegalStateException("无用户信息");
        }
        return platformInfo;
    }

    private static Map<String, Object> getShopInfo(String jwtToken) {
        Map<String, Object> platformInfo = JwtUtils.getPayLoadMap("shop_info", jwtToken);
        if (CollectionUtils.isEmpty(platformInfo)) {
            throw new IllegalStateException("无店铺信息");
        }
        return platformInfo;
    }

    public static String getShopId() {
        return BZValue.stringValue(getShopInfo(getToken()).get("id"));
    }

    private static String getAppUserId() {
        Object object;
        try {
            object = JwtUtils.getPayLoadInteger("id", getToken());
        } catch (Exception ignore) {
            object = "";
        }
        return BZValue.stringValue(object);
    }

    private static String getAppUserPhone() {
        Object userPhone;
        try {
            userPhone = JwtUtils.getPayLoadString("phone", getToken());
        } catch (Exception ignore) {
            userPhone = "";
        }
        return BZValue.stringValue(userPhone);
    }

    public static String getUserId() {
        Object userId;
        try {
            userId = getPlatformInfo(getToken()).get("user_id");
        } catch (Exception ignore) {
            userId = getAppUserId();
        }

        return BZValue.stringValue(userId);
    }

    public static String getUserName() {
        Object username;
        try {
            username = getPlatformInfo(getToken()).get("username");
        } catch (Exception ignore) {
            username = getUserPhone();
        }
        String s = BZValue.stringValue(username);
        if (StringUtils.isEmpty(s)) {
            s = getUserPhone();
        }
        return s;
    }

    public static String getUserPhone() {
        Object phone;
        try {
            phone = getPlatformInfo(getToken()).get("phone");
        } catch (Exception ignore) {
            phone = getAppUserPhone();
        }
        return BZValue.stringValue(phone);
    }
    public static String getAreaCode(){
        return "370112";
    }
}
